const express = require("express");
const router = express.Router();

// 导入控制器
const SupplierController = require("../controller/SupplierController");
const supplierController = new SupplierController(); // 实例化

// 引入权限检查中间件
const checkRoleMiddleware = require("../middleware/checkRoleMiddleware");

// 定义供应商模块不同操作所需角色列表
const SUPPLIER_ROLES = {
  CREATE: [1], // 创建供应商：超管
  UPDATE: [1], // 修改供应商详情：超管
  READ: [1, 2, 3], // 查看供应商：超管、库管、采购
  DELETE: [1], // 删除供应商：超管
};

// 供应商管理路由

// 获取供应商列表
router.get("/", (req, res) => {
  return supplierController.getSuppliers(req, res);
});

// 获取分类列表
router.get("/categories", (req, res) => {
  return supplierController.getSupplierCategories(req, res);
});

// 获取供应商详情
router.get("/:id", (req, res) => {
  return supplierController.getSupplierDetail(req, res);
});

// 新增供应商
router.post("/", checkRoleMiddleware(SUPPLIER_ROLES.CREATE), (req, res) => {
  return supplierController.createSupplier(req, res);
});

// 更新供应商信息
router.put("/:id", checkRoleMiddleware(SUPPLIER_ROLES.UPDATE), (req, res) => {
  return supplierController.updateSupplier(req, res);
});

// 软删除/停用供应商
router.delete("/:id", checkRoleMiddleware(SUPPLIER_ROLES.DELETE), (req, res) =>
  supplierController.deactivateSupplier(req, res)
);

module.exports = router;
